package edu.cmu.cs.stage3.alice.authoringtool.util;

import edu.cmu.cs.stage3.alice.authoringtool.AuthoringToolResources;

/**
 * @author Jason Pratt
 */
public class OneShotUndoableRedoable implements ContextAssignableUndoableRedoable {
	protected Object context;
	protected edu.cmu.cs.stage3.alice.core.Response redoResponse;
	protected edu.cmu.cs.stage3.alice.core.Response undoResponse;
	protected OneShotSimpleBehavior oneShotBehavior;
	protected edu.cmu.cs.stage3.alice.core.Scheduler scheduler;

	/**
	 * use this constructor if you already have a OneShotSimpleBehavior that's being run.
	 * if undo or redo are called while the bevavior is still being scheduled, its response
	 * will be stopped before the undo or redo occurs.
	 */
	public OneShotUndoableRedoable( edu.cmu.cs.stage3.alice.core.Response redoResponse, edu.cmu.cs.stage3.alice.core.Response undoResponse, OneShotSimpleBehavior oneShotBehavior, edu.cmu.cs.stage3.alice.core.Scheduler scheduler ) {
		this.redoResponse = redoResponse;
		this.undoResponse = undoResponse;
		this.oneShotBehavior = oneShotBehavior;
		this.scheduler = scheduler;
	}

	

	public void setContext( Object context ) {
		this.context = context;
	}

	public void undo() {
		oneShotBehavior.stopRunningResponse( AuthoringToolResources.getCurrentTime() );
		oneShotBehavior.setResponse( undoResponse );
		oneShotBehavior.start( scheduler );
	}

	public void redo() {
		oneShotBehavior.stopRunningResponse( AuthoringToolResources.getCurrentTime() );
		oneShotBehavior.setResponse( redoResponse );
		oneShotBehavior.start( scheduler );
	}

	public Object getAffectedObject() {
		return this;
	}

	public Object getContext() {
		return context;
	}

	public edu.cmu.cs.stage3.alice.core.Response getRedoResponse() {
		return redoResponse;
	}

	public edu.cmu.cs.stage3.alice.core.Response getUndoResponse() {
		return undoResponse;
	}

	public OneShotSimpleBehavior getOneShotBehavior() {
		return oneShotBehavior;
	}
}